January, 1996 - Vol. 3 No. 1


Listing C: WING.CPP
// Description: Implements the WinG 
// window class
//
// References: WinG help file, Game 
// Developer Magazine

#include "WinG.HPP"

void WinGWindow::InitWinG ( HWND ActiveWindow )
   {
   // Create a buffer to match the size of 
   // the active window:
   RECT ClientWin; HDC ScreenHDC;
   PALETTEENTRY SysPalette [ 256 ];
   short int N;
GetClientRect ( ActiveWindow, &ClientWin );
   WinWidth  = ClientWin.right  - ClientWin.left;
   WinHeight = ClientWin.bottom - ClientWin.top;
// Calculate the buffer's width (which must 
// be evenly divisible by 2):
   BufWidth = ( WinWidth + 3 ) & ( ~3 );
// Initialize the width and height of buffer:
   BufInfo.Header.biWidth  = WinWidth;
   BufInfo.Header.biHeight = WinHeight;
   BufInfo.Header.biClrUsed = 0;
   BufInfo.Header.biBitCount = 8;
   BufInfo.Header.biPlanes = 1;
   BufInfo.Header.biSize = sizeof BufInfo.Header;
   BufInfo.Header.biCompression = BI_RGB;
   BufInfo.Header.biSizeImage = 0;
if ( WinGRecommendDIBFormat ( ( LPBITMAPINFO ) 
       &BufInfo ) )
      {
      BufInfo.Header.biWidth = WinWidth;
      BufInfo.Header.biHeight *= WinHeight;
  // Get the DC of the active window:
      ScreenHDC = GetDC ( ActiveWindow );
   
      // Check for error:
      if ( ScreenHDC )
         {
         GetSystemPaletteEntries ( ScreenHDC, 0, 
           256, SysPalette );
     // Store the system palette in structure:
         for ( N = 0; N < 256; N++ )
             {
             BufInfo.ColorTable [ N ].rgbRed   = 
               SysPalette [ N ].peRed;
             BufInfo.ColorTable [ N ].rgbGreen = 
               SysPalette [ N ].peGreen;
             BufInfo.ColorTable [ N ].rgbBlue  = 
               SysPalette [ N ].peBlue;
             BufInfo.ColorTable [ N ].rgbReserved 
               = 0;
             }
     // Release the active window device context:
         ReleaseDC ( ActiveWindow, ScreenHDC );
         }
  // Create the off-screen DC:
      BufDC = WinGCreateDC ();
  // If an error did not occur....
      if ( BufDC )
         {
         // ..create the off-screen, WinG bitmap:
         BufBitmap = WinGCreateBitmap ( BufDC,
                       ( LPBITMAPINFO )&BufInfo, 
                       ( void * * ) &WinBuffer );
     if ( BufBitmap )
            {
            // Check for bottom-up bitmaps:
            if ( BufInfo.Header.biHeight > 0 )
               {
               WinBuffer += ( WinHeight - 1 ) * 
                 BufWidth;
               BufWidth = -BufWidth;
               }
        // Prepare the WinG bitmap:
            OldMonoBitmap = ( HBITMAP ) 
                            SelectObject ( BufDC,
                              BufBitmap );
            }
         // If an error occured....
         else {
              // ...deallocate device context:
              DeleteDC ( BufDC );
              BufDC = 0;
              WinBuffer = NULL;
              }
         }
      }
   }

WinGWindow::WinGWindow ( HWND ActiveWindow )
   {
   // Initialize the WinG bitmap to the 
   // dimensions of the
   // application's active window:
   InitWinG ( ActiveWindow );
   }

WinGWindow::WinGWindow ()
   {
   // Get the handle of the application's 
   // active window:
   HWND ActiveWindow = GetActiveWindow ();
   // Initialize the WinG bitmap to 
   // the dimensions of the active window: 
   InitWinG ( ActiveWindow );
   }

WinGWindow::~WinGWindow ()
  {
  // Delete the offscreen bitmap, 
  // selecting back in the original bitmap:
  if ( ( BufDC ) && ( BufBitmap ) )
     {
     SelectObject ( BufDC, OldMonoBitmap );
     DeleteObject ( BufBitmap );
     }
  // Delete the offscreen device context:
  if ( BufDC )
     DeleteDC ( BufDC );
  }

void WinGWindow::DispBuffer ()
   {
   // Get the handle of the application's 
   // active window:
   HWND ActiveWindow = GetActiveWindow ();
DispBuffer ( ActiveWindow );
   }

void WinGWindow::DispBuffer ( HWND ActiveWindow )
   {
   // Make sure the application has a valid 
   // active window:
   if ( ActiveWindow )
      {
      // Get the device context of the 
      // active window:
      HDC ActiveDC = GetDC ( ActiveWindow );
   // Check for error:
      if ( ActiveDC )
         {
         // Call WinG to blit the buffer (if you 
         // wanted to add scrolling background, 
         // this would be the place):
         WinGBitBlt ( ActiveDC, 0, 0, WinWidth, 
                      WinHeight, 
                      BufDC, 0, 0 );
         // Release the device context:
         ReleaseDC ( ActiveWindow, ActiveDC );
         }
      }
   }

Return to "Windows graphics programming - Implementing WinG in C++"


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.